package mm.chenchen.modules.reptile.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mm.chenchen.common.utils.MinioClientUtil;
import mm.chenchen.common.utils.MinioUtil;
import mm.chenchen.constant.ResultMsg;

import mm.chenchen.db.annotation.DataSource;
import mm.chenchen.log.annotation.YcLog;
import mm.chenchen.log.constant.YcLogLevel;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import mm.chenchen.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mm.chenchen.modules.reptile.entity.ReptileNetbianImage;
import mm.chenchen.modules.reptile.dao.ReptileNetbianImageDao;
import mm.chenchen.modules.reptile.service.ReptileNetbianImageService;
import mm.chenchen.utils.PageUtils;
import mm.chenchen.utils.PageModel;

import java.util.Map;
import mm.chenchen.modules.reptile.dao.model.request.ReptileNetbianImageQuery;
import org.springframework.util.ObjectUtils;


@Service("reptileNetbianImageService")
////@DataSource("爬虫资源库")
public class ReptileNetbianImageServiceImpl extends ServiceImpl<ReptileNetbianImageDao, ReptileNetbianImage> implements ReptileNetbianImageService {
    @Autowired
    private MinioClientUtil minioClientUtil;
    @YcLog(level = YcLogLevel.DEBUG)
    public R<PageModel<ReptileNetbianImage>> findPage(Page<Map> page, ReptileNetbianImageQuery param) {
        IPage<ReptileNetbianImage> pageData = baseMapper.findPage(page, param);
        if (pageData != null) {
            return R.ok().setData(new PageModel(pageData.getRecords(),pageData.getTotal(),pageData.getSize(),pageData.getCurrent()));
        } else {
            return R.error(ResultMsg.DATA_NOT_EXIT);
        }
    }

    public R<ReptileNetbianImage> getById(Integer id){
        return R.ok().setData(super.getById(id));
    };

    public R saveInfo(ReptileNetbianImage param){
        return R.ok().setData(super.save(param));
    };

    public R update(ReptileNetbianImage param){
            ReptileNetbianImage data=super.getById(param.getId());
            if(ObjectUtils.isEmpty(data)){
                return R.error(ResultMsg.DATA_NOT_EXIT);
            }
        return R.ok().setData(super.updateById(param));
    };

    public R deleteById(Integer id){
        ReptileNetbianImage img=getById(id).getData();
        if (!ObjectUtils.isEmpty(img)){
            MinioUtil.delete(minioClientUtil.getMinioClient(), img.getFileType(),img.getMinioId());
            super.removeById(id);
        }
        return R.ok().setData(super.removeById(id));
    }

    @Override
    @YcLog(level = YcLogLevel.INFO)
    public R getAllType() {
        QueryWrapper<ReptileNetbianImage> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("DISTINCT image_type,  " +
                " case image_type " +
                " when 'rili' then '日历'" +
                " when  'dongman' then '动漫' " +
                " when  'fengjing' then '风景' " +
                " when  'meinv' then 'asd' " +
                " ELSE" +
                "            '无'" +
                " END 'image_type_name' " );
        return R.ok().setData(list(queryWrapper));
    }
}